package com.gable.exhibition.thread;

import com.gable.common.util.StringUtil;
import com.gable.exhibition.dao.UserDao;
import com.gable.exhibition.util.HttpHelper;
import com.gable.exhibition.view.UserView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mj on 2018/3/1.
 * 报修短信推送
 */
public class RepairSendSmsThread implements Runnable{
    Logger logger = LoggerFactory.getLogger(this.getClass());

    private String hospitalName;        //医院名称

    private String deviceName;          //设备名称

    private String sn;      //序列号

    private UserDao userDao;

    private String sendSmsSystemForRepairUrl;   //维修推送给平台URL

    private String sendSmsSystemForUpkeepUrl;   //保养推送给平台URL

    public RepairSendSmsThread(String hospitalName,String deviceName,String sn,
                               UserDao userDao,String sendSmsSystemForRepairUrl,String sendSmsSystemForUpkeepUrl){
        this.hospitalName = hospitalName;
        this.deviceName = deviceName;
        this.sn = sn;
        this.userDao = userDao;
        this.sendSmsSystemForRepairUrl = sendSmsSystemForRepairUrl;
        this.sendSmsSystemForUpkeepUrl = sendSmsSystemForUpkeepUrl;
    }

    @Override
    public void run() {
        try {
            String url = sendSmsSystemForRepairUrl==null?sendSmsSystemForUpkeepUrl:sendSmsSystemForRepairUrl;
            UserView userView = new UserView();
            userView.setTag(1007L); //平台管理员
            List<String> mobiles = userDao.queryUserListByTag(userView);

            StringBuilder sb = new StringBuilder();
            if(mobiles != null && mobiles.size() > 0){
                for(String phone : mobiles){
                    if(StringUtil.isEmpty(phone))
                        continue;
                    sb.append(phone).append(",");
                }
                String mobile = sb.substring(0, sb.length() - 1);
                String message = hospitalName+"%&"+deviceName+"-"+sn;
                Map<String,String> hospitalMap = new HashMap<String,String>();
                hospitalMap.put("mobile",mobile);
                hospitalMap.put("message",message);
                String jsonResult = HttpHelper.postRequestResponseBodyAsString(url, hospitalMap);
                logger.info("报修推送给平台的短信设备名称："+deviceName+",短信发送结果:"+jsonResult);
            }else{
                logger.info("接收人电话为空");
            }
        } catch (Exception e) {
            logger.info("报修推送给平台的短信设备名称："+deviceName+",短信发送失败:"+e);
        }
    }
}
